package com.huizhonglm.diap.presenters.shop;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.model.shop.ShopInfo;
import com.huizhonglm.diap.api.model.shop.ShopListParams;
import com.huizhonglm.diap.core.config.Constants;
import com.huizhonglm.diap.core.location.LocationUtil;
import com.huizhonglm.diap.core.storage.model.LocationInfo;
import com.huizhonglm.diap.presenters.BasePresenter;
import com.huizhonglm.diap.ui.shop.iView.ShortcutShopIView;

import java.util.List;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-07 13:53
 */
public class ShortcutShopPresenter extends BasePresenter {

    private ShortcutShopIView iView;

    private int pageIndex;

    public ShortcutShopPresenter(ShortcutShopIView iView) {
        this.iView = iView;
    }

    public void onLoadShop(final String pullFlag, String categoryId){
        ShopListParams params = new ShopListParams(API.buildUri(API.SHORTCUTS_SHOP_LIST));
        LocationInfo loc = LocationUtil.getLocation();
        if (loc != null) {
            params.lat = loc.getLatitude();
            params.lng = loc.getLongitude();
        } else {
            params.lat = Constants.LATITUDE;
            params.lng = Constants.LONGITUDE;
        }
        params.categoryId = categoryId;
        switch (pullFlag) {
            case Constants.LOAD_INIT:
            case Constants.LOAD_DROP:
                pageIndex = 1;
                break;
            case Constants.LOAD_MORE:
                pageIndex ++;
                break;
        }
        params.pageNumber = pageIndex;
        API.http().post("shortcutShopList", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    List<ShopInfo> list = JSON.parseArray(result, ShopInfo.class);
                    if (!EmptyUtil.isEmpty(list)) {
                        iView.onLoadShopSuccess(pullFlag, list);
                    } else {
                        iView.onLoadShopEmpty(pullFlag);
                    }
                } else {
                    iView.onLoadShopEmpty(pullFlag);
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onLoadShopFailure(pullFlag);
            }
        });
    }
}
